<template>
	<om-page :title="pageTitle">
		<ot-sl-filter :clearTree="true" :ref="'slFilter'" :topFixed="true" :menuList="filterList" @result="onChangeFilter" />
		</ot-sl-filter>
		<uni-list v-if="showTitle.length">
			<uni-list-item  @click="openPopup()" style="">
				 <view slot="header" >
					<view class="tui-flex align-center om-font-size" v-for="(item,index) in showTitle" :key="index" >
						 <view class="tui-right show-label" style="font-size: 16px; width:250rpx;">{{deleteHtml(item.title)}}</view>
						 <p>{{'\xa0'}}</p>
						 <view class="" style="width:350rpx;">{{postData[item.field]}}</view>
					</view>
				 </view>
			</uni-list-item>
		</uni-list>
		<uni-popup  id="popup" ref="popup" :type="'center'">
			<view class="popup-cotent padding">
				<view class="margin popup-tip">
					请选择查询项目
				</view>
				<view  class="popup-item padding" @click="toDetail()">
					<text>岗位详情</text>
					<uni-icons :size="20" class="uni-icon-wrapper" color="#bbb" type="arrowright" />
				</view>
				<view  class="popup-item padding" @click="toTree()">
					<text>岗位关系图</text>
					<uni-icons :size="20" class="uni-icon-wrapper" color="#bbb" type="arrowright" />
				</view>
				<view class="padding-top padding-left">
					<text style="font-size: 16px">岗位任职人员</text>
				</view>
				<view class="padding-top">
					<scroll-view scroll-y="true"  style="height: 320rpx; border: 1px solid #b0b0b0;">
						<view  class="popup-item padding" v-for="(item,index) in userList" :key="index" @click="toUserInfo(item)">
							<text>{{item.username}}</text>
							<uni-icons :size="20" class="uni-icon-wrapper" color="#bbb" type="arrowright" />
						</view>
					</scroll-view>
				</view>
				<view class="margin flex justify-end" @click="cancel">
					<a class="cancel-btn">取消</a>
				</view>
			</view>
		</uni-popup>
	</om-page>	
</template>

<script>
	import {
		mapState
	} from 'pinia';
	import {
		useLoginUserStore
	} from '@/stores/login-user.js';
	import { ignoreHtml } from '@/static/js/common.js';
	export default{
		data(){
			return{
				pageTitle:'岗位信息查询',
				postData:{},
				postId:'',
				formTitle:[],
				showTitle:[],
				filterList: [{
					title: '单位',
					defaultTitle: '单位',
					detailTitle: '请选择单位',
					key: 'orgin',
					isSort: true,
					reflexTitle: true,
					defaultSelectedIndex: 0,
					detailList: [{
						'title': '单位',
						'value': ''
					}]
				},{
					title: '部门',
					defaultTitle: '部门',
					detailTitle: '请选择部门',
					key: 'instIds',
					isTree: true,
					reflexTitle: true,
					defaultSelectedText: '',
					defaultSelectedValue: '',
					detailList: [{
						'title': '部门',
						'value': ''
					}]
					
				},{
					title: '岗位',
					defaultTitle: '岗位',
					detailTitle: '请选择岗位',
					key: 'post',
					isSort: true,
					reflexTitle: true,
					defaultSelectedIndex: 0,
					detailList: [{
						'title': '岗位',
						'value': ''
					}]
				}],
				userList:[]
			}
		},
		provide() {
			return {
				searchKey: this
			}
		},
		computed: {
			...mapState(useLoginUserStore, ['orgShortName','orgId','salaryInstName','salaryInstId','salaryPosId','salaryPosName']),
			deleteHtml(){
				return function(val){
					return ignoreHtml(val)
				}
			}
		},
		methods:{
			toUserInfo(item){
				uni.navigateTo({
					url:'../temployee-search/temployee-search?userId='+item.id+'&userName='+item.username+'&instId='+this.postData.instId+'&instName='+this.postData.instName
				})
			},
			findUserlistByRequest(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficeView/findUserlistByRequest",
					{
						posId:id
					},
					'post'
				).then(res=>{
					_that.userList = res.value
				})
			},
			toDetail(){
				this.$refs.popup.close()
				let _that = this
				uni.navigateTo({
					url:'./post-detail'
				})
			},
			toTree(){
				this.$refs.popup.close()
				let _that = this
				uni.navigateTo({
					url:'./postTree?posId='+_that.postId
				})
			},
			openPopup(){
				this.$refs.popup.open()
			},
			cancel(){
				this.$refs.popup.close()
			},
			getPostDetail(id){
				let _that = this
				_that.om.request(
					'tposition/getPosById',
					{
						posId:id
					},
					'get',true
					).then(res=>{
						_that.postData = res.value
						let newShowTitle = [] 
						for(let item of _that.formTitle){
							if(_that.postData[item.field]){
								newShowTitle.push(item)
							}
						}
						_that.showTitle = newShowTitle
						_that.setCache(_that.formTitle,_that.postData)
					})
			},
			hasThis(arr,value){
				let _that = this
				for(let index in arr){
					if(value == arr[index].value){
						return index
					}
				}
				return ''
			},
			onChangeFilter(e,obj,isDestroy){
				let _that = this
				if(obj.key == 'instIds'){
					for(let index in _that.filterList){
						if(_that.filterList[index].key == obj.key){
							_that.salaryPostCheckedItem(_that.filterList[index].detailList,_that.filterList[index],e.instIds)
						}
					}
					_that.om.request(
						'tposition/findPostListByInst',
						{
							instId:e.instIds
						},
						'get',true
						).then(res=>{
							_that.filterList[2].detailList = _that.getNewArr('id','posName',res.value,_that.filterList[2].detailList)
							if(res.value.length >1){
								let index = _that.hasThis(_that.filterList[2].detailList,e.post)
								if(index){
									_that.postId = _that.filterList[2].detailList[index].value
									_that.filterList[2].defaultSelectedIndex = index
									_that.getPostDetail(_that.filterList[2].detailList[index].value)
									_that.findUserlistByRequest(_that.postId) 
								}else{
									_that.postId = _that.filterList[2].detailList[0].value
									_that.filterList[2].defaultSelectedIndex = 0
									_that.getPostDetail(_that.filterList[2].detailList[0].value)
									_that.findUserlistByRequest(_that.postId) 
								}
							}else{
								_that.filterList[2].defaultSelectedIndex = 0
								_that.filterList[2].detailList = [
									{
										'title': '岗位',
										'value': ''
									}]
								_that.showTitle = []
								_that.userList = []
							}
						})
				}else if(obj.key == 'post'){
					_that.postId = e.post
					_that.findUserlistByRequest(_that.postId) 
					let index = _that.hasThis(_that.filterList[2].detailList,e.post)
					if(index){
						_that.filterList[2].defaultSelectedIndex = index
					}else{
						_that.filterList[2].defaultSelectedIndex = 0
					}
					_that.getPostDetail(e.post)
				}
			},
			// tposition/getPosById
			// tposition/findPostListByInst
			changeArr(otit, ntit, oval, nval, data){
				for(let i=0; i<data.length; i++){
					if(otit){
						data[i][ntit] = data[i][otit]
					}
					if(oval){
						data[i][nval] = data[i][oval]
					}
				}
				return data;
			},
			getNewArr(value,title,dataList,detailList){
				let _that = this
				let newArr = [] 
				for(let index in dataList){
					let obj = {
						title:'',
						value:''
					}
					obj.title = dataList[index][title]
					obj.value = dataList[index][value]
					if(dataList[index].id == _that.salaryPosId){
						detailList.defaultSelectedIndex = index
					}
					newArr.push(obj)
				}
				return newArr
			},
			salaryPostCheckedItem(list,filterList,id){
				let _that = this
				for(let item of list){
					if(item.id == (id || _that.salaryInstId)){
						item.checked = true
						if(filterList){
							filterList.defaultSelectedText = item.id
							filterList.defaultSelectedValue = item.text
							filterList.title = item.text
						}
					}else{
						item.checked = false
					}
					if(item.children && item.children.length){
						_that.salaryPostCheckedItem(item.children,filterList,id)
					}
				}
				return
			},
			setCache(title,data){
				uni.setStorage({
					key:'postDetailTitleCache',
					data:title
				})
				uni.setStorage({
					key:'postDetailDataCache',
					data:data
				})
			},
			initLoad(){
				let _that = this
				uni.showLoading({
					mask: true,
					title: '请稍候...'
				})
				let arr = [ 
					_that.om.request("torganization/getOrgList",{},'get',true),
					_that.om.request("tinstitution/getComboTree",{},'get',true),
					_that.om.request(
						'tposition/findPostListByInst',
						{
							instId:_that.salaryInstId
						},
						'get',true
						),
						_that.om.request(
							'tposition/getPosById',
							{
								posId:_that.salaryPosId
							},
							'get',true
							),
						_that.om.request(
							'ttableColumn/getGridModel',
							{
								tableMark:'KEY_30'
							},
							'get',true
							),
						_that.om.request(
							"orgInstPosUserOfficeView/findUserlistByRequest",
							{
								posId:_that.salaryPosId
							},
							'post',true
							)
				 ]
				 Promise.all(arr).then(res=>{
					_that.filterList[0].detailList = _that._.cloneDeep(_that.changeArr('shortName', 'title', 'id', 'value', res[0]))
					let list = _that._.cloneDeep(res[1])
					_that.salaryPostCheckedItem(list,_that.filterList[1])
					_that.filterList[1].detailList = list
					if(_that.$refs.slFilter){
						_that.$refs.slFilter.resetMenuList(_that.filterList)
					}
					_that.filterList[2].detailList = _that.getNewArr('id','posName',res[2].value,_that.filterList[2].detailList)
					for(let index in  _that.filterList[2].detailList){
						if(_that.filterList[2].detailList[index].value == _that.salaryPosId){
							 _that.filterList[2].defaultSelectedIndex = index
						}
					}
					_that.postData = res[3].value
					res[4].value.frozenColumns[0].push(...res[4].value.columns[0])
					_that.formTitle = res[4].value.frozenColumns[0]
					_that.setCache(_that.formTitle,_that.postData)
					for(let item of res[4].value.frozenColumns[0]){
						if(_that.postData[item.field]){
							_that.showTitle.push(item)
						}
					}
					_that.userList = res[5].value
					uni.hideLoading()
				 })
			}
		},
		onUnload() {
			uni.removeStorage({
				key:'postDetailTitleCache'
			})
			uni.removeStorage({
				key:'postDetailDataCache'
			})
		},
		onLoad(options){
			let _that = this
			_that.pageTitle = options.title;
			uni.setNavigationBarTitle({
				title: _that.pageTitle
			});
			_that.filterList[0].detailList = [{
												'title': _that.orgShortName,
												'value': _that.orgid
											}]
			_that.filterList[1].defaultSelectedText = _that.salaryInstId
			_that.filterList[1].defaultSelectedValue = _that.salaryInstName
			_that.filterList[1].title = _that.salaryInstName
			_that.initLoad()
		}
	}
</script>

<style scoped>
	.footerSize{
		font-size: 14px;
	}
	.popup-item{
		height:50px;
		background-color: #FFFF;
		font-size: 16px;
		text-align: center;
		border-bottom: 1px solid #EEEEEE;
		display: flex;
		justify-content: space-between;
	}
	.popup-cotent{
		width: 300px;
		background-color: white;
		border-radius: 10px;
	}
	.popup-tip{
		font-size: 16px;
		color:#b0b0b0 ;
	}
	.cancel-btn{
		text-align: center;
		display: block;
		color: #0081ff;
		background-color: #cce6ff;
		height: 36px;
		line-height: 36px;
		width: 72px;
		margin: 15px auto 0;
	}
</style>                                                  
